package com.aaa.vip.service.impl;

import com.aaa.common.constant.ConstantUtil;
import com.aaa.common.entity.FrontUser;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.util.TokenUtil;
import com.aaa.common.vo.Page;
import com.aaa.common.vo.UserRegisterStatisticsVO;
import com.aaa.vip.dao.FrontUserDao;
import com.aaa.vip.service.FrontUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * (FrontUser)表服务实现类
 *
 * @author makejava
 * @since 2025-09-20 09:05:34
 */
@Service("frontUserService")
public class FrontUserServiceImpl implements FrontUserService {
    @Resource
    private FrontUserDao frontUserDao;
    @Resource
    private HttpServletRequest request;

    @Override
    public Integer updatePassword(String phone, String password) {
        String salt = UUID.randomUUID().toString();
        Sha512Hash newPasswordHash = new Sha512Hash(password, salt, ConstantUtil.SHRIO_HASH_ITERATIONS);
        String newPassword = newPasswordHash.toString();
        return frontUserDao.updatePassword(phone, newPassword, salt);
    }

    @Override
    public FrontUser selectByPhone(String phone) {
        return frontUserDao.selectByPhone(phone);
    }

    @Override
    public FrontUser queryByIdUseToken() {
        String userId = TokenUtil.getUserId(request);
        return frontUserDao.queryById(Integer.valueOf(userId));
    }

    @Override
    public List<UserRegisterStatisticsVO> getUserRegisterStatistics() {
        return frontUserDao.selectUserRegisterByMonth();
    }

    @Override
    public int getTotalUserCount() {
        return frontUserDao.selectTotalUserCount();
    }
    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public FrontUser queryById(Integer userId) {
        return this.frontUserDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<FrontUser> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        FrontUser frontUser = page.getData();
        return new PageInfo(frontUserDao.queryAllByLimit(frontUser));
    }

    /**
     * 新增数据
     *
     * @param frontUser 实例对象
     * @return 实例对象
     */
    @Override
    public FrontUser insert(FrontUser frontUser) {
        frontUser.setUserName(frontUser.getPhone());
        frontUser.setPhone(frontUser.getPhone());
        frontUser.setStatus("0");
        String salt = UUID.randomUUID().toString();
        frontUser.setSalt(salt);
        Sha512Hash sha512Hash = new Sha512Hash(frontUser.getPassword(), salt, ConstantUtil.SHRIO_HASH_ITERATIONS);
        frontUser.setPassword(sha512Hash.toString());
        this.frontUserDao.insert(frontUser);
        return frontUser;
    }

    /**
     * 修改数据
     *
     * @param frontUser 实例对象
     * @return 实例对象
     */
    @Override
    public FrontUser update(FrontUser frontUser) {
        this.frontUserDao.update(frontUser);
        return this.queryById(frontUser.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.frontUserDao.deleteById(userId) > 0;
    }
}
